//
//  suggestionController.swift
//  SchoolDemo
//
//  Created by rimi on 16/10/8.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class suggestionController: UIViewController,UITextViewDelegate{
    
    var label1: UILabel?
    var label2: UILabel?
    var label3: UILabel?
    var label4: UILabel?
    var text1: UITextField?
    var text2: UITextView?
    var button: UIButton?
    var sumtBtn: UIButton?
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //导航栏
        setUI()
        
   
        
        //标题和输入意见
        headtext()
        
        //隐藏label
        initUITextView()
        //右侧的清除按钮
        btn()
        //提交按钮
        submitButton()
    }
 
    
}


//MARK: - 标题框 输入框 输入清除按钮
extension suggestionController {
    //标题
    func headtext() {
        text1 = UITextField(frame: CGRect(x: 0, y: 65, width: UIScreen.main.bounds.width, height: 64))
        text1?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
     
        text1?.placeholder = "  请输入标题"
        //清除按钮
        text1?.clearButtonMode = .whileEditing
        
        text1?.font = UIFont.systemFont(ofSize: 16)
        view.addSubview(text1!)
        
        label4 = UILabel(frame: CGRect(x: 0, y: 65, width: UIScreen.main.bounds.width, height: 64))
        label4?.font = UIFont.systemFont(ofSize: 15)
        label4?.textAlignment = .left
        view.addSubview(label4!)
   
    }

    func initUITextView() {

        text2 = UITextView(frame: CGRect(x: 0, y: 135, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 3))
        text2?.font = UIFont.systemFont(ofSize: 15)
        text2?.alpha = 1
        text2?.returnKeyType = .done
        text2?.delegate = self
        
        self.view.addSubview(text2!)
        label3 = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
        label3?.font = UIFont.systemFont(ofSize: 15)
        label3?.textAlignment = .left
        label3?.text = "  请输入您宝贵的意见，帮助我们做的更好！"
        label3?.textColor = UIColor().colorWithHex(0xb2b2b2)
        label3?.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        text2?.addSubview(label3!)
        
    }
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        return true
    }
    func textViewDidChange(_ textView: UITextView) {
        if (text2?.text.isEmpty)! {
            label3?.isHidden = false//显示
            button?.isHidden = true

        }else {
            label3?.isHidden = true //隐藏
            button?.isHidden = false
        }
    }
    func  textViewShouldEndEditing(_ textView: UITextView) -> Bool {
          return true
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        if (text2?.text.isEmpty)! {
            label3?.isHidden = false//显示
            button?.isHidden = false
        }else {
            label3?.isHidden = true //隐藏
            button?.isHidden = true
        }
    }
   
    @objc(textView:shouldChangeTextInRange:replacementText:) func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            text2?.resignFirstResponder()
        }
        return true
    }
    func btn() {
     
        button = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 22 , y: 10, width: 13, height: 13))
        button?.setImage(UIImage(named:"清除"), for: .normal)
    
        button?.addTarget(self, action: #selector(dispelBtn), for: UIControlEvents.touchUpInside)
        button?.isHidden = true
        text2?.addSubview(button!)
    }
    func dispelBtn() {
        text2?.text = ""
        button?.isHidden = true
        label3?.isHidden = false
    }
    
}

//MARK: - 提交按钮
extension suggestionController {

    func submitButton() {
        sumtBtn = UIButton(frame: CGRect(x: 15, y: UIScreen.main.bounds.height / 2 + 50, width: UIScreen.main.bounds.width - 30, height: 44))
        sumtBtn?.backgroundColor = UIColor().colorWithHex(0x009ee7)
        sumtBtn?.setTitle("提交", for: .normal)
        sumtBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 19)
        sumtBtn?.addTarget(self, action: #selector(sumclickBtn), for: .touchUpInside)
        view.addSubview(sumtBtn!)
    }
    
   func sumclickBtn() {
    
        //接口地址
        let url = "ZH-schoolname-S-submitJXOpinion"
        //参数
        let key = userInfo["key"] as! String
    
        let parpam = ["key":key, "title":text1?.text ?? "", "content":text2?.text ?? ""]
    
        //请求数据
        HSNetWorkTools.shared.downloadData(url: url, parameters: parpam) { (data, err) in
            //判断网络是否我有问题
            guard let data = data as? NSDictionary else {
                
                    HSAlertView.bottomAlertView("网络异常，请重新确认")
                
                return
                
            }
            switch data["result"] as! Int{
            case 0:
                HSAlertView.bottomAlertView("感谢您的建议")
                self.text1?.text = ""
                self.text2?.text = ""
                self.button?.isHidden = true
                self.label3?.isHidden = false
            case 1:
                HSAlertView.bottomAlertView("失败，请重发")
            default:
                break
            }
            
        }
    }
}




//MARK: - 导航栏
extension suggestionController {
    //上方导航栏
    func setUI() {
        view.backgroundColor = UIColor().colorWithHex(0xf0f0f0)
        
        label1 = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
        label1?.backgroundColor = UIColor().colorWithHex(0x009ee7)
        label1?.text = "意见反馈"
        label1?.textColor = UIColor.white
        label1?.textAlignment = NSTextAlignment.center
        label1?.font = UIFont.systemFont(ofSize: 20)
        view.addSubview(label1!)
        
        let btn = UIButton()
        btn.frame = CGRect(x: 0, y: 13, width: 44, height: 44)
        btn.setImage(UIImage(named:"返回按钮"), for: UIControlState.normal)
        btn.center.x = self.view.bounds.width / 7
        btn.addTarget(self, action: #selector(clickedBtn), for: UIControlEvents.touchUpInside)
        view.addSubview(btn)
        
    }
    //左上角箭头监听回调
    func clickedBtn() {
        dismiss(animated: true, completion: nil)
    }
    
    
}
